﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Configuration;
using BLL;
using Model;
using System.Security.Cryptography;
using System.Text;

/// <summary>
///SendDX 的摘要说明
/// </summary>
public class SendDX
{
    DXLogBO dbo = new DXLogBO();
    MerchantsBO mbo = new MerchantsBO();
    sdk.WebServiceSoapClient sms = new sdk.WebServiceSoapClient();
    public SendDX()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //


    }

    /// <summary>
    /// 发送短信
    /// </summary>
    /// <param name="Message">信息内容</param>
    /// <param name="Phone">手机号码</param>
    /// <param name="ToUserName">商户微信号或ID</param>
    /// <returns>发送结果</returns>
    public string Send(string Message, string Phone, string ToUserName)
    {
        Merchants m = null;
        try
        {
            try
            {
                int id = int.Parse(ToUserName);
                m = mbo.GetModel(id);
            }
            catch (Exception)
            {
                m = mbo.GetModel(ToUserName);
            }
        }
        catch (Exception)
        {

        }
        DXService.FocusSMSWebServiceSoapClient c = new DXService.FocusSMSWebServiceSoapClient();
        string userName = "service@crmfocussend.com";
        string pwd = "123456a";
        string encodedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, FormsAuthPasswordFormat.SHA1.ToString("G"));
        string taskName = "短信即时发送_" + DateTime.Now.Ticks.ToString();
        string mobileList = Phone;
        string content = Message + "【扑满科技】";
        if (m != null)
            content += "  " + m.DX;
        string priority = "1";
        string addrial = "1";
        string msg = c.SendNote(userName, encodedPwd, taskName, mobileList, content, priority, addrial);
        try
        {
            DXLog d = new DXLog();
            d.JG = msg;
            d.MID = ToUserName;
            d.NR = content;
            d.Time = DateTime.Now;
            d.ToUser = mobileList;
            dbo.Add(d);
        }
        catch (Exception)
        {


        }
        return msg;
    }




    public string Send(string Message, string Phone, int? hyzt)
    {
        if (hyzt == 0) //在职
        {
            string msg = "";
            try
            {

                Merchants m = mbo.GetModel(1);
                if (m != null)
                    Message += "  " + m.DX;
                //DXService.FocusSMSWebServiceSoapClient c = new DXService.FocusSMSWebServiceSoapClient();
                //string userName = "service@crmfocussend.com";
                //string pwd = "123456a";
                //string encodedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, FormsAuthPasswordFormat.SHA1.ToString("G"));
                //string taskName = "短信即时发送_" + DateTime.Now.Ticks.ToString();
                //string mobileList = Phone;            
                //string content = Message + "【扑满科技】";
                //string priority = "1";
                //string addrial = "1";
                ////msg = c.SendNote(userName, encodedPwd, taskName, mobileList, content, priority, addrial);
                //msg = Mt("SDK-BBX-010-19253", getMD5("SDK-BBX-010-19253e-5ec2-["), mobileList, content, "", "", "");
                si_SMSInsert_obService dx = new si_SMSInsert_obService();
                dt_SMSInsert_req dxr = new dt_SMSInsert_req();
                dxr.SMS_ITEM = new SMS_ITEM[] { new SMS_ITEM() { CONTENT = Message, MESSAGEID = "0000001", MESSAGETYPE = "SPA_WX_SM", MOBILE = Phone, MSGFORMAT = "8", SRCNUMBER = "106900293716" } };
                dt_SMSInsert_res dts = dx.si_SMSInsert_ob(dxr);
                try
                {
                    if (dts != null)
                    {
                        msg = "发送成功";
                        DXLog d = new DXLog();
                        d.JG = msg;
                        d.NR = Message;
                        d.Time = DateTime.Now;
                        d.ToUser = Phone;
                        dbo.Add(d);
                    }
                }
                catch (Exception)
                {
                }

            }
            catch (Exception)
            {
                return "发送失败";
            }
            return msg;
        }
        else
            return "会员已离职";
    }

    /// <summary>
    /// 获取md5码
    /// </summary>
    /// <param name="source">待转换字符串</param>
    /// <returns>md5加密后的字符串</returns>
    public string getMD5(string source)
    {
        string result = "";
        try
        {
            MD5 getmd5 = new MD5CryptoServiceProvider();
            byte[] targetStr = getmd5.ComputeHash(UnicodeEncoding.UTF8.GetBytes(source));
            result = BitConverter.ToString(targetStr).Replace("-", "");
            return result;
        }
        catch (Exception)
        {
            return "0";
        }

    }


    /// <summary>
    /// 普通群发短信mt方法
    /// </summary>
    /// <param name="sn">序列号</param>
    /// <param name="pwd">密码要MD5(SN+PWD)加密，取32位大写</param>
    /// <param name="mobiles">手机号列表</param>
    /// <param name="content">短信内容</param>
    /// <param name="ext">扩展码</param>
    /// <param name="stime">定时时间</param>
    /// <param name="rrid">流水号</param>
    /// <returns>发送结果</returns>
    public string Mt(string sn, string pwd, string mobiles, string content, string ext, string stime, string rrid)
    {
        string result = sms.mt(sn, pwd, mobiles, content, ext, stime, rrid);
        return CheckResult(result);
    }


    static string CheckResult(string result)
    {
        if (result.StartsWith("-"))
        {
            return "发送失败！" + GetWhy(result) + "。返回值是：" + result;
        }
        else
        {
            return "发送成功。流水号是：" + result;
        }
    }

    static string GetWhy(string code)
    {
        switch (code)
        {
            case "-2":
                return "序列号未注册或加密不对";

            case "-4":
                return "余额不足";
            case "-6":
                return "参数错误，请检测所有参数";
            case "-7":

                return "权限受限";

            case "-9":

                return "扩展码权限错误";
            case "-10":

                return "内容过长，短信不得超过500个字符";
            case "-20":

                return "相同手机号，相同内容重复提交";
            case "-22":

                return "Ip鉴权失败";

            default:
                return "错误，请调试程序";
        }
    }
}


